package com.twb.auth.controller;

import com.twb.auth.entity.twbApiGroup.TwbApiGroup;
import com.twb.auth.entity.twbApiGroup.dto.GetTwbApiGroupByIdDto;
import com.twb.auth.entity.twbApiGroup.dto.SearchTwbApiGroupDto;
import com.twb.auth.entity.twbApiGroup.dto.SearchTwbApiGroupTreeDto;
import com.twb.auth.entity.twbApiGroup.vo.SaveTwbApiGroupVo;
import com.twb.auth.entity.twbApiGroup.vo.SearchTwbApiGroupTreeVo;
import com.twb.auth.entity.twbApiGroup.vo.SearchTwbApiGroupVo;
import com.twb.auth.entity.twbApiGroup.vo.UpdateTwbApiGroupByIdVo;
import com.twb.auth.service.ITwbApiGroupService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/** 
 * @Title  TwbApiGroupController
 * @Description API分组信息
 
 * @author DSY
 * @date 2021-05-13 14:35:38
 */ 
@RestController
@RequestMapping("/auth/twbApiGroup")
@Api(tags = "API分组信息",value="TwbApiGroupController")
public class TwbApiGroupController extends BaseController{
	@Autowired
	ITwbApiGroupService twbApiGroupService;
	
	/**
	 * @Title  search
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@GetMapping("/search")
	@ApiOperation(value="分页查询")
	public Result<TwbPageInfo<SearchTwbApiGroupDto>> search(SearchTwbApiGroupVo vo){
		TwbPageInfo<SearchTwbApiGroupDto> result = twbApiGroupService.searchTwbApiGroup(vo);
		return Results.successWithData(result);
	}
	
	/**
	 * @Title  getById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@GetMapping("/getById")
	@ApiOperation(value="根据ID获取详情")
	public Result<GetTwbApiGroupByIdDto> getById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		TwbApiGroup twbApiGroup = twbApiGroupService.getById(id);
		if(twbApiGroup == null) {
			return Results.failure("数据不存在");
		}
		GetTwbApiGroupByIdDto dto = new GetTwbApiGroupByIdDto();
		BeanUtils.copyNotNullProperties(twbApiGroup, dto);
		return Results.successWithData(dto);
	}
	
	/**
	 * @Title  save
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@PostMapping("/save")
	@ApiOperation(value="保存信息")
	public Result<String> save(@RequestBody @Valid SaveTwbApiGroupVo vo){
		boolean result = twbApiGroupService.saveTwbApiGroup(vo);
		if(result) {
			return Results.success("保存成功");
		}else {
			return Results.failure("保存失败");
		}
	}
	
	/**
	 * @Title  updateById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@PutMapping("/updateById")
	@ApiOperation(value="更新信息")
	public Result<String> updateById(@RequestBody @Valid UpdateTwbApiGroupByIdVo vo){
		boolean result = twbApiGroupService.updateTwbApiGroupById(vo);
		if(result) {
			return Results.success("更新成功");
		}else {
			return Results.failure("更新失败");
		}
	}
	
	
	/**
	 * @Title  deleteById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@PostMapping("/deleteById")
	@ApiOperation(value="删除信息")
	public Result<String> deleteById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		boolean result = twbApiGroupService.deleteTwbApiGroupById(id);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	
	/**
	 * @Title  deleteBatchByIds
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
	@Auth(verifyToken = true)
	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value="批量删除信息")
	public Result<String> deleteBatchByIds(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String ids){
		boolean result = twbApiGroupService.deleteTwbApiGroupByIds(ids);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}
	
	/**
	 * @Title searchTwbApiGroupTree
	 * @Description api树结构查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021年5月13日 下午5:15:34
	 */
	@GetMapping("/searchTree")
	@ApiOperation(value = "api分组列表树查询")
	public Result<List<SearchTwbApiGroupTreeDto>> searchTwbApiGroupTree(SearchTwbApiGroupTreeVo vo) {
		List<SearchTwbApiGroupTreeDto> list = twbApiGroupService.searchTwbApiGroupTree(vo);
		return Results.successWithData(list);
	}
	
	@PostMapping("/refreshApis")
	@ApiOperation(value="批量更新Api信息")
	public Result<String> refreshApis(
			@NotBlank(message = "项目Id不能为空")
			@RequestParam
			@ApiParam(value="项目Id",name="projectId",required=true)
			String projectId,
			
			@NotNull(message = "类型不能为空")
			@RequestParam
			@ApiParam(value="类型（1：包名；2：类名）",name="type",required=true)
			String type,
			
			@NotBlank(message = "包名/类名不能为空")
			@RequestParam
			@ApiParam(value="包名/类名（多个用英文逗号隔开）",name="packagesOrClasses",required=true)
			String packagesOrClasses){
		boolean result = twbApiGroupService.refreshApis(projectId,type, packagesOrClasses);
		if(result) {
			return Results.success("更新成功");
		}else {
			return Results.failure("更新失败");
		}
	}
	
	/**
	 * @Title  logicDeleteById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicDeleteById")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicDeleteById(
//			@NotBlank
//			@RequestParam
//			String id){
//		TwbApiGroup record = twbApiGroupService.get(id);
//		if(null == record) {
//			return Results.failure("信息不存在");
//		}
//		record = new TwbApiGroup();
//		record.setId(id);
//		twbApiGroupService.setUpdateInfo(record);
//		record.setIsdel(1);
//		int result = twbApiGroupService.updateSelective(record);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}

	
	/**
	 * @Title  logicBatchDeleteByIds
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicBatchDeleteByIds")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicBatchDeleteByIds(
//			@NotBlank
//			@RequestParam
//			String ids){
//		int result = twbApiGroupService.logicBatchDeleteTwbApiGroupByIds(ids);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}

	
	/**
	 * @Title  exportExcel
	 * @Description 导出excel
	 * @param exportExcelVo
	 * @param searchVo
	 * @param request
	 * @return
	 * @author DSY
	 * @date 2021-05-13 14:35:38
	 */
//	@Auth(verifyURL=false)
//	@GetMapping("/exportExcel")
//	@ApiOperation(value="导出excel")
//	public void exportExcel(@RequestBody SearchTwbDataDicVo vo, HttpServletResponse response){
//		twbApiGroupService.exportExcel(vo, response);
//	}
}